var Chart = {
    STEPS: 25,
    DEFAULT_MAX_X: 1,
    DEFAULT_MAX_Y: 1,
    WIDTH_PX: 400,
    HEIGHT_PX: 300,
    STYLE: "surface",
    V_RATIO: 0.5
};


Chart.R3Chart = function() {
    var capacitor;
    var chapa;
    var graphInf;
    var graphSup;

    var options = {
        width: Chart.WIDTH_PX + 'px',
        height: Chart.HEIGHT_PX + 'px',
        style: Chart.STYLE,
        showPerspective: true,
        showGrid: true,
        showShadow: false,
        keepAspectRatio: true,
        verticalRatio: Chart.V_RATIO
    };

    this.setCapacitor = function(c) {
        capacitor = c;
    };

    this.drawChapaSup = function(element) {
        var self = this;

        google.load("visualization", "1");
        google.setOnLoadCallback(function() {
            var data = self.fillDataTable(capacitor.getChapaSup());
            graphSup = new links.Graph3d(element);
            graphSup.draw(data, options);
        });
    };

    this.drawChapaInf = function(element) {
        var self = this;

        google.load("visualization", "1");
        google.setOnLoadCallback(function() {
            var data = self.fillDataTable(capacitor.getChapaInf());
            graphInf = new links.Graph3d(element);
            graphInf.draw(data, options);
        });
    };

    this.updateChapaSup = function() {
        var data = this.fillDataTable(capacitor.getChapaSup());
        graphSup.redraw(data);
    };

    this.updateChapaInf = function() {
        var data = this.fillDataTable(capacitor.getChapaInf());
        graphInf.redraw(data);
    };

    this.fillDataTable = function(chapa) {
        var result = new google.visualization.DataTable();

        result.addColumn('number', 'x');
        result.addColumn('number', 'y');
        result.addColumn('number', 'z');

        xStep = capacitor.getMaxX() / Chart.STEPS;
        yStep = capacitor.getMaxY() / Chart.STEPS;
        for (var x = 0; x < capacitor.getMaxX(); x+= xStep) {
            for (var y = 0; y < capacitor.getMaxY(); y+= yStep) {
                var z = chapa.getZAt(x, y);
                result.addRow([x, y, z]);
            }
        }

        return result;
    }
};
